﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace PM.Web
{
    public class Common
    {
        public static void CheckUserOnline()
        {
            if (HttpContext.Current.Session["EmpId"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('您没有登录！或者登录已经超时了！');</script>");
                HttpContext.Current.Response.End();
            }
        }

        public static void CheckGovOnline()
        {
            if (HttpContext.Current.Session["GovId"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('您没有登录！或者登录已经超时了！');window.location.href='admin_gov_login.aspx';</script>");
                HttpContext.Current.Response.End();
            }
        }

        public static int GetCommunityCode()
        {
            int communityId = 0;

            if (HttpContext.Current.Cache["communityId"] != null)
            {
                communityId = Convert.ToInt32(HttpContext.Current.Cache["communityId"]);
            }
            else
            {
                string sDomain = HttpContext.Current.Request.Url.Host;
                DataAccess.PmData dal = new DataAccess.PmData();
                communityId = dal.GetCommunityIdByDomain(sDomain);
                HttpContext.Current.Cache["communityId"] = communityId;
            }
            //return communityId;
            return 1;//本地测试用
        }
        //public static void JsAlert(System.Web.UI.Page p, string msg)
        //{
        //    p.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>art.dialog({icon: 'warning',skin: 'aero',content: '" + msg + "'})</script>");
        //}

        //public static void JsAlert(System.Web.UI.Page p, string msg,bool refershParent)
        //{
        //    p.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>art.dialog('" + msg + "', function(){alert('yes');}););</script>");
        //}

        public static void JsAlert(System.Web.UI.Page p, string msg)
        {
            p.ClientScript.RegisterStartupScript(typeof(string), "alert", "<script>alert('" + msg + "');</script>");
        }

        public static void JsAlert(System.Web.UI.Page p, string msg, bool refershParent)
        {
            string script = "<script>alert('" + msg + "');";
            if (refershParent) script += "document.getElementById('hidflag').value='1';";
            script+="</script>";
            p.ClientScript.RegisterStartupScript(typeof(string), "alert", script);
        }

        /// <summary>
        /// 弹出提示信息
        /// </summary>
        /// <param name="p">页面</param>
        /// <param name="sMsg">信息内容</param>
        public static void Alert(Page p, string sMsg)
        {
            Alert(p, sMsg, string.Empty, false, false);
        }
        /// <summary>
        /// 弹出提示信息并跳转
        /// </summary>
        /// <param name="p">页面</param>
        /// <param name="sMsg">信息内容</param>
        /// <param name="goUrl">跳转地址</param>
        public static void Alert(Page p, string sMsg, string goUrl)
        {
            Alert(p, sMsg, goUrl, false, false);
        }
        /// <summary>
        /// 弹出提示信息返回
        /// </summary>
        /// <param name="p">页面</param>
        /// <param name="sMsg">信息内容</param>
        /// <param name="goBack">是否返回</param>
        public static void Alert(Page p, string sMsg, bool goBack)
        {
            Alert(p, sMsg, string.Empty, goBack, false);
        }
        public static void Alert(Page p, string sMsg, bool close, bool goBack)
        {
            Alert(p, sMsg, string.Empty, goBack, close);
        }
        /// <summary>
        /// 弹出提示信息
        /// </summary>
        /// <param name="p">页面</param>
        /// <param name="sMsg">信息内容</param>
        /// <param name="goUrl">跳转地址</param>
        /// <param name="goBack">是否返回</param>
        public static void Alert(Page p, string sMsg, string goUrl, bool goBack, bool close)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendFormat("<script language=javascript>alert('{0}');", sMsg);
            if (close)
                sb.Append("window.close();");
            if (goUrl != string.Empty)
                sb.AppendFormat("window.location.href='{0}';", goUrl);
            if (goBack)
                sb.Append("history.go(-1);");
            sb.Append("</script>");
            p.ClientScript.RegisterClientScriptBlock(p.GetType(), "alert0", sb.ToString());
        }


        private static char[] constant =
        {   
            '0','1','2','3','4','5','6','7','8','9', 
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };
        public static string GetRandomNumber(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }

        /// <summary>
        /// 除去HTML标记
        /// </summary>
        /// <param name="sHtml">HTML输入源</param>
        /// <returns></returns>
        public static string ParseTags(string sHtml)
        {
            return System.Text.RegularExpressions.Regex.Replace(sHtml, "<[^>]*>", string.Empty);
        }
    }
}